/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-12-08     周童欢       the first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <gpio.h>
#include "key.h"
#include "iis.h"
#include "audio.h"
#include <math.h>

#define AUDIO_BUFFER_SIZE 1000
#define PI 3.14159265358979

uint32_t audio_buffer[AUDIO_BUFFER_SIZE];

void audio_task(void* parameter)
{
    // 生成正弦波 PCM 数据
    for (int i = 0; i < AUDIO_BUFFER_SIZE; i++)
    {
//        audio_buffer[i] = (uint16_t)(2048 + 2047 * sin(2 * PI * i / AUDIO_BUFFER_SIZE)); // 16 位数据
          audio_buffer[i] = (uint16_t)(2048 + 2047 * sin(2 * PI * i / AUDIO_BUFFER_SIZE))<<8; // 16 位数据
//        audio_buffer[i] = 1000;
//        audio_buffer[i] *= 1;
    }
//    for (int i = 0; i < AUDIO_BUFFER_SIZE/2; i++)
//    {
//          audio_buffer[i] = 0x22300;
//    }

    while (1)
    {
        rt_sem_take(Key_Handle.sem_tx, RT_WAITING_FOREVER);

        rt_kprintf("playing...........\r\n");
//        for (int i = 0; i < 10000000; ++i)
//        {
//            I2S_Transmit((uint16_t *)audio_buffer, AUDIO_BUFFER_SIZE);
//        }
//                I2S3_Transmit_DMA(audio_buffer, AUDIO_BUFFER_SIZE);
//        rt_thread_delay(100000);
        rt_kprintf("playing over\r\n");
        rt_sem_release(Key_Handle.sem_rx);
        rt_thread_mdelay(500);
    }
}
